package com.jcfk.eam.controller.equ;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.main.EamInspectionTaskDTO;
import com.jcfk.eam.domain.query.main.EamInspectionTaskQuery;
import com.jcfk.eam.domain.query.main.EamPointCheckOrderQuery;
import com.jcfk.eam.enums.AssetKind;
import com.jcfk.eam.enums.TaskKind;
import com.jcfk.eam.service.main.EamInspectionTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 设备巡检任务
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-12-04
 */
@Api(tags = {"设备巡检任务管理"})
@RestController
@RequestMapping("equRouteInspectionTask")
public class EquRouteInspectionTaskController {
    @Autowired
    private EamInspectionTaskService eamInspectionTaskService;

    /**
     * 分页列表
     */
    @ApiOperation("分页查询设备巡检任务")
    @PostMapping("/queryByPage")
    public RetPage<EamInspectionTaskDTO> queryByPage(@RequestBody ReqPageParam<EamInspectionTaskQuery> requestParam) {
//        //设置默认排序
//        if(requestParam.getSort().getOrderItem()==null){
//            ReqSortParam<EamInspectionTaskQuery> sortParam=new ReqSortParam();
//            sortParam.setColumn("lastUpdateTime");
//            sortParam.setOrderBy("desc");
//            requestParam.setSort(sortParam);
//        }
        DataPage<EamInspectionTaskDTO> page = new DataPage<EamInspectionTaskDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamInspectionTaskDTO.class);
        requestParam.getData().setInspectionKind(TaskKind.ROUTE_INSPECTION.getValue());
        requestParam.getData().setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        page = eamInspectionTaskService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 分页查询我的设备巡检任务
     */
    @ApiOperation("分页查询我的设备巡检任务")
    @PostMapping("/queryMyList")
    public RetPage<EamInspectionTaskDTO> queryMyList(@RequestBody ReqPageParam<EamInspectionTaskQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<EamInspectionTaskQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }

        DataPage<EamInspectionTaskDTO> page = new DataPage<EamInspectionTaskDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamInspectionTaskDTO.class);
        requestParam.getData().setInspectionKind(TaskKind.ROUTE_INSPECTION.getValue());
        requestParam.getData().setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        page = eamInspectionTaskService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  设备巡检任务
     * 根据id 查询详情
     */
    @ApiOperation("查询设备巡检任务")
    @GetMapping("/get/{id}")
    public RetResult<EamInspectionTaskDTO> get(@PathVariable("id") String id) {
            EamInspectionTaskDTO item = eamInspectionTaskService.get(id);
        return RetResult.ok(item);
    }

    /**
     *  查询设备巡检任务查看信息
     */
    @ApiOperation("查询设备巡检任务查看信息")
    @GetMapping("/getView/{id}")
    public RetResult<EamInspectionTaskDTO> getView(@PathVariable("id") String id) {
        EamInspectionTaskDTO item = eamInspectionTaskService.getView(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理设备巡检任务
     */
    @ApiOperation("保存设备巡检任务")
    @PostMapping("/save")
    @RequestLogHeader(modular = "设备巡检任务管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamInspectionTaskDTO eamInspectionTask) {
        // 数据校验
        RetResult ret = eamInspectionTaskService.isValid(eamInspectionTask);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamInspectionTask.getId())){
            // 新增
            eamInspectionTaskService.insert(eamInspectionTask);
        } else{
            // 更新
            eamInspectionTaskService.update(eamInspectionTask);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除设备巡检任务
     */
    @ApiOperation("批量删除设备巡检任务")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "设备巡检任务管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamInspectionTaskDTO eamInspectionTask) {
        return RetResult.ok(eamInspectionTaskService.removeBigBatch(eamInspectionTask.getIds()));
    }


    /**
     *  删除设备巡检任务
     */
    @ApiOperation("删除设备巡检任务")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "设备巡检任务管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamInspectionTaskService.deleteItem(id);
        return RetResult.ok();
    }
}
